package edu.umn.csci5511.facedetection.image;

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.util.Arrays;

import edu.umn.csci5511.facedetection.colorspace.ColorRGB;
import edu.umn.csci5511.facedetection.colorspace.ColorYCbCr;
import edu.umn.csci5511.facedetection.colorspace.ColorYCbCr1;

public class ImageData {
	private BufferedImage _image = null;
	private int _imageHeight;
	private int _imageWidth;
	private ColorRGB[][] _imageRGBVals;
	private ColorYCbCr[][] _imageYCbCr;
	private ColorYCbCr1[][] _imageYCbCr1;

	public ImageData(BufferedImage image) {
		this._image = image;
		this._imageHeight = image.getHeight();
		this._imageWidth = image.getWidth();
		this._imageRGBVals = new ColorRGB[_imageHeight][_imageWidth];
		this._imageYCbCr = new ColorYCbCr[_imageHeight][_imageWidth];
		this._imageYCbCr1 = new ColorYCbCr1[_imageHeight][_imageWidth];
	}

	public BufferedImage getImageCopy() {

		ColorModel cm = this._image.getColorModel();
		boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
		WritableRaster raster = this._image.copyData(null);
		return new BufferedImage(cm, raster, isAlphaPremultiplied, null);

	}

	public ColorRGB[][] getProcessedRGBValsCopy() {
		ColorRGB[][] tempCopy = new ColorRGB[this._imageRGBVals.length][];
		for (int i = 0; i < this._imageRGBVals.length; i++) {
			tempCopy[i] = Arrays.copyOf(this._imageRGBVals[i], this._imageRGBVals[i].length);
		}
		return tempCopy;
	}

	public ColorYCbCr[][] getProcessedYCbCrValsCopy() {
		ColorYCbCr[][] tempCopy = new ColorYCbCr[this._imageYCbCr.length][];
		for (int i = 0; i < this._imageYCbCr.length; i++) {
			tempCopy[i] = Arrays.copyOf(this._imageYCbCr[i], this._imageYCbCr[i].length);
		}
		return tempCopy;
	}

	public ColorYCbCr1[][] getProcessedYCbCr1ValCopy() {
		ColorYCbCr1[][] tempCopy = new ColorYCbCr1[this._imageYCbCr1.length][];
		for (int i = 0; i < this._imageYCbCr1.length; i++) {
			tempCopy[i] = Arrays.copyOf(this._imageYCbCr1[i], this._imageYCbCr1[i].length);
		}
		return tempCopy;
	}

	public void addToRGBVals(int x_pixel, int y_pixel, byte r, byte g, byte b) {
		this._imageRGBVals[x_pixel][y_pixel] = new ColorRGB(r, g, b);
	}

	public void addToYCbCrVals(int x_pixel, int y_pixel, float y, float cb, float cr) {
		this._imageYCbCr[x_pixel][y_pixel] = new ColorYCbCr(y, cb, cr);
	}

	public void addToYCbCr1Vals(int x_pixel, int y_pixel, byte y1, byte cb1, byte cr1) {
		this._imageYCbCr1[x_pixel][y_pixel] = new ColorYCbCr1(y1, cb1, cr1);
	}

}
